using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class P2 : MonoBehaviour
{

    [Header("打击配置")]
    [SerializeField]
    private GameObject HitCon;
    [Header("红")]
    [SerializeField]
    private Vector2 normalLandHitOffset = new Vector2(1,0);
    [SerializeField]
    private Vector2 normalLandHitSize = new Vector2(1,0.25f);
    
    [Header("黄色")]
    [SerializeField]
    private Vector2 normalAirHitOffset = new Vector2(1,0);
    [SerializeField]
    private Vector2 normalAirHitSize = new Vector2(1,0.25f);

    [Header("接触地面投影配置")]
    [SerializeField]
    private Vector2 groundCheckVec;
    [SerializeField]
    private Transform groundCheck;
    [SerializeField]
    private LayerMask whatIsGround;
    [SerializeField]
    public Animator anim;
    [SerializeField]
    public float speed = 1.25f;

    [Header("跳跃配置")]
    [SerializeField]
    private float jumpDurationTime = 0.25f;
    [SerializeField]
    private float jumpInitVelocity = 0.25f;
    [SerializeField]
    private float jumpHoldVelocity = 0.5f;
    private float currPressJumpTime = 0;
    private bool canUp = false;

    private Rigidbody2D rb;
    private BoxCollider2D cc;

    
    /// 通过玩家输入的速度
    private float inputXVelocity;
    private Dir faceDir = Dir.RIGHT;


    #region 打击碰撞体变量
    public BoxCollider2D NormalLandHitCollider;
    public BoxCollider2D NormalAirHitCollider;
    #endregion


    private void Awake() {
        NormalLandHitCollider = HitCon.transform.Find("NormalLandHit").GetComponent<BoxCollider2D>();
        NormalLandHitCollider.size = normalLandHitSize;
        NormalLandHitCollider.gameObject.SetActive(false);

        NormalAirHitCollider = HitCon.transform.Find("NormalAirHit").GetComponent<BoxCollider2D>();
        NormalAirHitCollider.size = normalAirHitSize;
        NormalAirHitCollider.gameObject.SetActive(false);
    }

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        cc = GetComponent<BoxCollider2D>();
    }



    // Update is called once per frame
    void Update()
    {
        inputXVelocity = 0;
        if (Input.GetKey(KeyCode.A)) {
            inputXVelocity += -speed;
        }
        if (Input.GetKey(KeyCode.D)) {
            inputXVelocity += speed;
        }

        if (Input.GetKey(KeyCode.Space)) {
            // fsm.HandleEvent("idle2jumpUp");
            // fsm.HandleEvent("run2jumpUp");
        }

        var stateInfo = anim.GetCurrentAnimatorStateInfo(0);

        if (stateInfo.IsName("LqIdle") || stateInfo.IsName("LqSlow")) {
            //优先接受攻击输入
            if (Input.GetButton("Fire1")) {
                anim.SetTrigger("2landHit");
                return;
            }

            if (inputXVelocity != 0) {
                anim.SetTrigger("2run");
                rb.velocity = new Vector2(inputXVelocity, 0);
                SetFaceByXV();
            }
            else {
                if (Input.GetKey(KeyCode.Space)) {
                    anim.SetTrigger("2jumpUp");
                    currPressJumpTime = 0;
                    rb.velocity = new Vector2(0, rb.velocity.y + jumpInitVelocity);//初始给跳跃方向的速度
                }
            }
        }

        if (stateInfo.IsName("LqRun")) {
            
            if (Input.GetButton("Fire1")) {//优先接受攻击输入
                anim.SetTrigger("2landHit");
                rb.velocity = Vector2.zero;
                return;
            }

            if (Input.GetKey(KeyCode.Space)) {//其次接受跳跃输入
                anim.SetTrigger("2jumpUp");
                currPressJumpTime = 0;
                if (inputXVelocity == 0) {
                    rb.velocity = new Vector2(0, jumpInitVelocity);//给跳跃方向的速度
                }
                else {
                    rb.velocity = new Vector2(inputXVelocity, jumpInitVelocity);//给跳跃方向的速度
                }
                return;
            }

            if (inputXVelocity == 0) {
                anim.SetTrigger("2slow");
            }
            else {
                SetFaceByXV();
            }
            rb.velocity = new Vector2(inputXVelocity, rb.velocity.y);

        }

        if (stateInfo.IsName("LqJumpUp") || stateInfo.IsName("LqFall") || stateInfo.IsName("LqFalling")) {
            if (Input.GetButton("Fire1")) {//优先接受攻击输入
                Debug.Log("Fire press");
                anim.SetTrigger("2airHit");
            }
        }


        
    }

    private void FixedUpdate() {
        var stateInfo = anim.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.IsName("LqAir")) {
            // 检查是否触地
            rb.velocity = new Vector2(inputXVelocity, rb.velocity.y);
            if (IsStandGround()) {
                //落地
                Debug.Log("落地啦！");
                if (inputXVelocity == 0) {
                    anim.SetTrigger("air2idle");
                }
                else {
                    anim.SetTrigger("air2run");
                }
            }
        }

        if (stateInfo.IsName("LqJumpUp")) {
            rb.velocity = new Vector2(inputXVelocity, rb.velocity.y);

            SetFaceByXV();

            currPressJumpTime += Time.deltaTime;
            if (Input.GetKey(KeyCode.Space) && currPressJumpTime < jumpDurationTime) {//当按下跳跃键，同时按下跳跃时间没超过【跳跃上升时间】
                rb.velocity = new Vector2(inputXVelocity, rb.velocity.y + jumpHoldVelocity);//持续给跳跃方向的速度
            }
            else {
                currPressJumpTime = jumpDurationTime;
            }

            if (rb.velocity.y < 0) {
                anim.SetTrigger("2air");
            }
        }

        if (stateInfo.IsName("LqFall") || stateInfo.IsName("LqFalling")) {
            // 检查是否触地
            rb.velocity = new Vector2(inputXVelocity, rb.velocity.y);

            SetFaceByXV();
            if (IsStandGround()) {
                //落地
                Debug.Log("落地啦！");
                if (inputXVelocity == 0) {
                    anim.SetTrigger("2idle");
                }
                else {
                    anim.SetTrigger("2run");
                }
            }
        }

        if (stateInfo.IsName("LqAirHit")) {
            rb.velocity = new Vector2(inputXVelocity, rb.velocity.y);
            if (IsStandGround()) {
                //落地
                Debug.Log("落地啦！");
                if (inputXVelocity == 0) {
                    anim.SetTrigger("2idle");
                }
                else {
                    anim.SetTrigger("2run");
                }
            }
        }
    }  

    private bool IsStandGround() {
        return Physics2D.OverlapBox(groundCheck.position, groundCheckVec,0, whatIsGround);
    }

    private void SetFace(Dir dir)
    {
        if (faceDir != dir) {
            transform.Rotate(0.0f, 180.0f, 0.0f);
            faceDir = dir;
        }
    }
    ///通过X方向速度V来设置face
    private void SetFaceByXV()
    {
        if (inputXVelocity == 0)
            return;
        if (inputXVelocity > 0) {
            SetFace(Dir.RIGHT);
        }
        else {
            SetFace(Dir.LEFT);
        }
    }

    
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(groundCheck.position, groundCheckVec);
    }

    private void OnDrawGizmosSelected() {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(normalLandHitOffset, normalLandHitSize);
        
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireCube(normalAirHitOffset, normalAirHitSize);
    }



    #region 帧事件
    public void OnFrameLandHit() {
        NormalLandHitCollider.gameObject.SetActive(true);
        NormalLandHitCollider.transform.localPosition = normalLandHitOffset;
    }
    public void OnFrameLandHitOver() {
        NormalLandHitCollider.gameObject.SetActive(false);
    }

    public void OnFrameAirHit() {
        NormalAirHitCollider.gameObject.SetActive(true);
        NormalAirHitCollider.transform.localPosition = normalAirHitOffset;
    }
    public void OnFrameAirHitOver() {
        NormalAirHitCollider.gameObject.SetActive(false);
    }
    public void OnAirHitOver() {
        NormalAirHitCollider.gameObject.SetActive(false);
        if (rb.velocity.y > 0) {
            anim.SetTrigger("2jumpUp");
        }
        else {
            anim.SetTrigger("2air");
        }
    }
    #endregion
}
